<?php

function CopyTagsToComments(&$getid3->info) {

    // Copy all entries from ['tags'] into common ['comments'] and ['comments_html']

    if (@$getid3->info['tags']) {
        
        foreach ($getid3->info['tags'] as $tag_type => $tag_array) {
        
            foreach ($tag_array as $tag_name => $tagdata) {
        
                foreach ($tagdata as $key => $value) {
        
                    if (!empty($value)) {
        
                        if (empty($getid3->info['comments'][$tag_name])) {

                            // fall through and append value

                        } elseif ($tag_type == 'id3v1') {

                            $new_value_length = strlen(trim($value));
                            foreach ($getid3->info['comments'][$tag_name] as $existing_key => $existing_value) {
                                $oldvaluelength = strlen(trim($existing_value));
                                if (($new_value_length <= $oldvaluelength) && (substr($existing_value, 0, $new_value_length) == trim($value))) {
                                    // new value is identical but shorter-than (or equal-length to) one already in comments - skip
                                    break 2;
                                }
                            }

                        } else {

                            $new_value_length = strlen(trim($value));
                            foreach ($getid3->info['comments'][$tag_name] as $existing_key => $existing_value) {
                                $oldvaluelength = strlen(trim($existing_value));
                                if (($new_value_length > $oldvaluelength) && (substr(trim($value), 0, strlen($existing_value)) == $existing_value)) {
                                    $getid3->info['comments'][$tag_name][$existing_key] = trim($value);
                                    break 2;
                                }
                            }

                        }
        
                        if (empty($getid3->info['comments'][$tag_name]) || !in_array(trim($value), $getid3->info['comments'][$tag_name])) {
                            $getid3->info['comments'][$tag_name][] = trim($value);
                            if (isset($getid3->info['tags_html'][$tag_type][$tag_name][$key])) {
                                $getid3->info['comments_html'][$tag_name][] = $getid3->info['tags_html'][$tag_type][$tag_name][$key];
                            }
                        }
                    }
                }
            }
        }
    }
}


?>